package com.example.leakcanarytest;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;

import com.bugsnag.android.Bugsnag;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

/**
 * @author COCO
 * @date 2019/4/2
 */
public class MyApplication extends Application {

    private RefWatcher refWatcher;


    @Override
    public void onCreate() {
        super.onCreate();
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        refWatcher = LeakCanary.install(this);
        Bugsnag.init(this);


        if (Build.VERSION.SDK_INT >= 24) {
            //Detect when the calling application exposes a {@code file://} {@link android.net.Uri}* to another app.
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            builder.detectFileUriExposure();

            //ThreadPolicy：用于监测线程部分，监测 主线程中是否访问网络、主线程中是否读写磁盘等.
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectAll()//监测所有内容
                    .penaltyLog()//违规对log日志
                    .detectNetwork()//监测主线程中是否存在有访问网络
                    .penaltyDeath()//一旦触发任何违规操作就直接Crash掉程序
                    .penaltyDialog()//一旦触发违规操作，就弹出违规信息对话框
                    .build());
        }
    }

    public static RefWatcher getRefWatcher(Context context) {
        MyApplication application = (MyApplication) context.getApplicationContext();
        return application.refWatcher;
    }

}
